<?php
class Index extends Controller
{
	public function __construct()
	{
		parent::__construct();
		
		$this->indexAct();
	}
	
	/**
	 * @param
	 */
	public function archives()
	{
		$aid = isset($_REQUEST['aid']) ? intval($_REQUEST['aid']) : exit('lose param');
		$archive = ArchivesModel::get($aid);
		
		$this->hot(10);
		$this->hotByCat($archive['cid'], 10);
		$this->locat($archive['cid']);
		$this->assign('archive', $archive);
		$this->display('archives.tpl');
	}
	
	public function _default()
	{
		$archives = array();
		$cats = CatalogModel::getList();
		foreach ($cats as $cat){
			$archives[$cat['id']] = ArchivesModel::getList("cid={$cat['id']}", 1, 30);
		}
		$this->assign('archives', $archives);
		$this->display('index.tpl');
	}
	
	public function catalog()
	{
		$cid = isset($_REQUEST['cid']) ? intval($_REQUEST['cid']) : exit('lose param');
		$page = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1;
		$pageSize = isset($_REQUEST['pageSize']) ? intval($_REQUEST['pageSize']) : 30;
		$where = 'cid=' . $cid;
		$archives = ArchivesModel::getList($where, $page, $pageSize);
		$count = ArchivesModel::getCount($where);
		$pageBreak = Util::page($count, $pageSize);
		
		$this->hot(10);
		$this->hotByCat($cid, 10);
		$this->locat($cid);
		$this->assign('catalogInfo', CatalogModel::get($cid));
		$this->assign('archives', $archives);
		$this->assign('pageBreak', $pageBreak);
		$this->display('catalog.tpl');
	}
	
	public function channel()
	{
		$cid = isset($_REQUEST['cid']) ? intval($_REQUEST['cid']) : exit('lose param');
		$cats = CatalogModel::getList($cid);
		foreach ($cats as $cat){
			$archives[$cat['id']] = ArchivesModel::getList("cid={$cat['id']}", 1, 30);
		}
		
		$this->hot(10);
		$this->hotByCat($cid, 10);
		$this->locat($cid);
		$this->assign('catalogInfo', CatalogModel::get($cid));
		$this->assign('subCatalog', $cats);
		$this->assign('archives', $archives);
		$this->display('channel.tpl');
	}
	
	public function info()
	{
		$cid = isset($_REQUEST['cid']) ? intval($_REQUEST['cid']) : exit('lose param');
		$catalog = CatalogModel::get($cid);
		
		$this->hot(10);
		$this->hotByCat($cid, 10);
		$this->locat($cid);
		$this->assign('catalogInfo', $catalog);
		$this->display('info.tpl');
	}
	
	public function hot($count=10)
	{
		$hots = ArchivesModel::getHot($count);
		$this->assign('hots', $hots);
	}
	
	public function hotByCat($cid, $count=10)
	{
		$hots = ArchivesModel::getHotByCat($cid, $count);
		$this->assign('catHots', $hots);
	}
	
	public function picArticle()
	{
		$picArticle = ArchivesModel::getPicArticle(10);
		$this->assign('picArchives', $picArticle);
	}
	
	public function archivesByCat($cid, $page=1, $count=30)
	{
		$where = "cid={$cid}";
		$news = ArchivesModel::getList($where, $page, $count);
		$this->assign('news', $news);
	}
	
	private function locat($cid)
	{
		$cats = CatalogModel::getList(0);
		$locat = array();
		foreach ($cats as $item){
			if ($item['id']==$cid) {
				array_push($locat, $item);
				break;
			}
		}
		
		//向前找
		$tmp = $item;
		while($tmp['pid']){
			foreach ($cats as $pre){
				if ($tmp['pid']==$pre['id']){
					$tmp = $pre;
					array_unshift($locat, $pre);
				}
			}
		}
		/*
		//向后找
		$tmp = $item;
		$flag = true;
		while ($flag){
			$flag = false;
			foreach ($cats as $next){
				if ($tmp['id'] == $next['pid']){
					$flag = true; 
					$tmp = $next;
					array_push($locat, $tmp);
				}
			}
		}*/
		$this->assign('locat', $locat);
	}
	
	private function indexAct()
	{
		if (isset($_GET['cid']) && $_GET['cid']){
			$this->assign('cid', $_GET['cid']);
		}else{
			$this->assign('cid', 0);
		}
		
		$configs = ConfigModel::getList();
		foreach ($configs as $item){
			$tmpConfig[$item['name']] = $item['value'];
		}
		$this->picArticle();
		$this->getCatalogList();
		$this->getLinks();
		
		$this->assign('config', $tmpConfig);
	}
	
	private function getCatalogList($cid='')
	{
		$catalogList = CatalogModel::getList();
		$tmpCatalogs = array();
		foreach ($catalogList as $item){
			$tmpCatalogs[$item['id']] = $item;
		}
		if ($cid) $this->getTopId($tmpCatalogs, $cid);
		$this->assign('catalogs', $tmpCatalogs);
		return $tmpCatalogs;
	}
	
	private function getLinks()
	{
		$linkList = LinkModel::getList();
		$this->assign('links', $linkList);
		
	}
	
	private function getTopId($catalogs, $cid)
	{
		while ($catalogs[$cid]['pid']){
			$pid = $catalogs[$cid]['pid'];
			$cid = $pid;
		}
		$this->assign('topId', $cid);
		return $cid;
	}
}